import * as transferService from '../../services/transfer';
import { getJoberNum, qryById } from '../../services/shelfDown';
import { queryAssetType } from '../../services/assetType';
import { qryDepts } from '../../services/department';
import { qryAllUser } from '../../services/user';
import { qryUserByDept } from '../../services/user';
import { queryAssetBrand, queryAssetModelAll } from '../../services/assetModel';
import { queryRoomListByParams } from '../../services/tRoom';
import { qryRackList } from '../../services/equipment';
import {queryDataCenter} from '../../services/tRoom'

export default {
  namespace: 'transfer',
  state: {
    list: [],
    assetTypeTree: [],
    deptTree: [],
    roomList: [],
    assetBrandList: [],
    assetModelList:[],
    userList: [],
    deptUserList:[],
    deptList: [],
    rackList: [],
    assetList: [],
    current: 1,
    currentSize: 10,
    total: 0,
    modalVisible: false,
    modalType: 'create',
    newKey: '',
    detailList: [],
    editDetailList: [],
    currentItem: {},
    viewModalVisible: false,
    viewItem: {},
    assetItem: {},
    viewDetailList: [],
    transferId: "",
    editViewItem: {},
    editTransferDetailItem: {},
    detailModalVisible: false,
    secondModalVisible: false,
    assetCurrent: 1,
    assetCurrentSize: 10,
    assetTotal: 0,
    doTransferViewItem:{},
    transferDetailList:[],
    qryAssetItem:{},
    dataCenterList:[],
  },
  reducers: {
    updateState (state, { payload }) {
      return { ...state, ...payload}
    },
    queryDataCenterSuc(state, { payload: dataCenterList }) {
      return { ...state, dataCenterList };
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryListByParamSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    changeDetailList(state, { payload }) {
      return { ...state, ...payload, secondModalVisible: false };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {
      return { ...state, assetBrandList };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    queryUserSuc(state, { payload: userList }) {
      return { ...state, userList };
    },
    qryUserByDeptSuc(state, { payload: deptUserList }) {
      return { ...state, deptUserList };
    },
    qryRackListSuc(state, { payload: rackList }) {
      return { ...state, rackList };
    },

    queryDeptsSuc(state, { payload: deptList }) {
      return { ...state, deptList };
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    getJoberNumSuc(state, { payload }) {
      return { ...state, transferId: payload };
    },

    showModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', modalVisible: true }
    },
    hideModal(state, { payload }) {
      return { ...state, ...payload, modalVisible: false, detailList: [], newKey: new Date().getTime() + '' }
    },

    showViewModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', viewModalVisible: true }
    },
    hideViewModal(state) {
      return { ...state, newKey: new Date().getTime() + '', viewModalVisible: false, }
    },
    showCreateDetailModal(state, { payload }) {
      return { ...state, ...payload, detailModalVisible: true }
    },
    hideCreateDetailModal(state, { payload }) {
      return { ...state, ...payload, detailModalVisible: false }
    },
    showSecondModal(state, { payload }) {
      return { ...state, ...payload,secondModalVisible: true }
    },
    hideSecondModal(state) {
      return { ...state, secondModalVisible: false }
    },

    showAddTransferDetailModal(state, { payload }) {
      return { ...state, ...payload, addTransferDetailModalVisible: true }
    },
    hideAddTransferDetailModal(state) {
      return { ...state, addTransferDetailModalVisible: false }
    },

    updateDetaiListSuc(state, { payload }) {
      return { ...state, ...{ detailList: payload } }
    },
    updateEditDetaiListSuc(state, { payload }) {
      return { ...state, ...{ editDetailList: payload } }
    },

    showEditViewModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', editModalVisible: true }
    },
    hideEditViewModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', editModalVisible: false }
    },

    showDoTransferViewModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', doTransferModalVisible: true }
    },
    hideDoTransferViewModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', doTransferModalVisible: false }
    },

    showEditDetailViewModal(state, { payload }) {
      return { ...state, ...payload, editDetailModalVisible: true }
    },
    hideEditDetailViewModal(state) {
      return { ...state, editDetailModalVisible: false }
    },
  },
  effects: {
    *queryDataCenter({ payload },{ call, put }) {
      const { body } = yield call(queryDataCenter,payload);
      yield put({ type: 'queryDataCenterSuc', payload: body.data });
    }, 

    *queryUser({ payload }, { call, put }) {
      const { body } = yield call(qryAllUser);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *qryUserByDept({ payload }, { call, put }) {
      const { body } = yield call(qryUserByDept,payload);
      yield put({ type: 'qryUserByDeptSuc', payload: body.data });
    },

    *queryAssetType({ payload }, { call, put }) {
      const { body } = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryRoom({ payload }, { call, put }) {
      const { body } = yield call(queryRoomListByParams, payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *qryRackList({ payload }, { call, put }) {
      const { body } = yield call(qryRackList, payload);
      yield put({ type: 'qryRackListSuc', payload: body.data });
    },
    *queryAssetBrand({ payload }, { call, put }) {
      const { body } = yield call(queryAssetBrand, payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    },

    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(queryAssetModelAll,payload);
      yield put({ type: 'updateState', payload: {assetModelList:body.data} });
    },
    *qryAssetById({ payload }, { call, put }) {
      // const { body } = yield call(transferService.qryAssetById, payload);
      yield put({ type: 'showAddTransferDetailModal', payload: { assetItem:payload } });
      yield put({ type: 'qryUserByDept',payload:{deptId:payload.useBranchId_}});
      yield put({ type: 'queryRoom',payload: {}});
      yield put({type: 'queryDataCenter'});
      yield put({ type: 'qryRackList', payload:payload.roomId_});
    },
    *queryAssetList({ payload }, { call, put }) {
      yield put({ type: 'querAssetListByParam', payload: { roomId: payload.roomId} });
      yield put({ type: 'showSecondModal', payload: { qryAssetItem: payload } });
    },
    *queryList({ payload }, { call, put }) {
      const { body } = yield call(transferService.queryTransferList, payload);
      yield put({ type: 'queryListSuc', payload: { list: body.data, current: body.current, currentSize: body.size, total: body.iTotalRecords } });
    },

    *onAddViewItem({ payload }, { call, put }) {
      const { body } = yield call(getJoberNum, payload);
      yield put({ type: 'getJoberNumSuc', payload: body.data, });
      yield put({ type: 'showModal' });
      yield put({ type: 'queryAssetType' });
      yield put({ type: 'queryAssetBrand', payload: {}, });
      yield put({ type: 'queryDepts' });
    },
    *qryById({ payload }, { call, put }) {
      const { body } = yield call(transferService.qryById, payload);
      yield put({
        type: 'showEditViewModal', payload: { editViewItem: body.data, editDetailList: body.data.transferDetailList },
      });
      yield put({ type: 'queryAssetType' });
      yield put({ type: 'queryAssetBrand', payload: {}, });
      yield put({ type: 'queryDepts' });
    },


    *onDoTransferViewItem({ payload }, { call, put }) {
      const { body } = yield call(transferService.qryById, payload);
      yield put({
        type: 'showDoTransferViewModal', payload: { doTransferViewItem: body.data, transferDetailList: body.data.transferDetailList },
      });
    },

    *onViewItem({ payload }, { call, put }) {
      const { body } = yield call(transferService.qryById, payload);
      yield put({
        type: 'showViewModal', payload: { viewItem: body.data, },
      });
    },


    *updateAddDetaiList({ payload }, { call, put }) {
      yield put({ type: 'updateDetaiListSuc', payload });
      yield put({ type: 'hideAddTransferDetailModal' });
    },


    *onEditDetailViewItem({ payload }, { call, put }) {
      const { body } = yield call(transferService.qryDetailById, payload);
      yield put({ type: 'showEditDetailViewModal', payload: { editTransferDetailItem: body.data } });
      yield put({ type: 'queryUser' });
      yield put({ type: 'queryRoom' });
      yield put({ type: 'qryRackList', payload: 0 });
    },

    *updateEditDetaiList({ payload }, { call, put }) {
      yield put({ type: 'updateEditDetaiListSuc', payload });
      yield put({ type: 'hideEditDetailViewModal' });
    },

    *querAssetListByParam({ payload }, { call, put }) {
      const { body } = yield call(transferService.querAssetListByParam, payload);
      yield put({ type: 'queryListByParamSuc', payload: { assetList: body.data, assetCurrent: body.current, assetCurrentSize: body.size, assetTotal: body.iTotalRecords } });
    },

    *create({ payload }, { call, put }) {
      const { body } = yield call(transferService.create, payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload' });
    },
    *edit({ payload }, { call, put }) {
      const { body } = yield call(transferService.edit, payload);
      yield put({ type: 'hideEditViewModal' });
      yield put({ type: 'reload' });
    },
    *doTransfer({ payload }, { call, put }) {
      const { body } = yield call(transferService.doTransfer, payload);
      yield put({ type: 'hideDoTransferViewModal' });
      yield put({ type: 'reload' });
    },
    *remove({ payload }, { call, put }) {
      const { body } = yield call(transferService.remove, payload.id);
      yield put({ type: 'queryList', payload: payload.search });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload: { 'name': '' } });
    },

    *queryDepts({ payload }, { call, put }) {
      const { body } = yield call(qryDepts);
      yield put({ type: 'queryDeptsSuc', payload: body.data });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/transfer') {
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryRoom',payload: {} });
          dispatch({ type: 'queryList', payload: {} });
        }
      });
    },
  },
};
